package com.hariro.adapter;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hariro.fragment.New_Detail_Fragment;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.qianfeng.project.R;

public class ListView_Fragment_New_Adapter extends BaseAdapter {

	private List<Map<String, Object>> list = null;
	private Context context;
	private int count = 0;

	private PopupWindow popupWindow_Below;
	private View popupView_Below;
	private Button button_popup_share_cancel;
	private Button radioButton_popup_share_pyq;
	private Button radioButton_popup_share_wxhy;
	private Button radioButton_popup_share_qqhy;
	private Button radioButton_popup_share_wb;
	private Button radioButton_popup_share_qqkj;
	private View view_new;
	private RelativeLayout relativeLayout_fragment;
	private RelativeLayout relativeLayout_details;
	private ImageView imageView_new_back;
	private ImageView imageView_new_refresh;
	private TextView textView_Tab_new;

	// X_Utils
	private LayoutInflater inflater = null;
	public BitmapUtils bitmapUtils = null;

	public ListView_Fragment_New_Adapter(Context context,
			List<Map<String, Object>> list, View view) {
		this.list = list;
		this.context = context;
		view_new = view;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// 获取缓存路径;
		String diskCachePath = context.getExternalCacheDir().getAbsolutePath();
		bitmapUtils = new BitmapUtils(context, diskCachePath)
				.configMemoryCacheEnabled(true)
				.configDefaultCacheExpiry(1024 * 1024 * 4)
				.configDefaultBitmapMaxSize(350, 350)
				.configDefaultLoadingImage(R.drawable.fragment_loading_default)
				.configDefaultBitmapConfig(Config.ALPHA_8)
				.configDefaultLoadFailedImage(
						R.drawable.umeng_socialize_share_pic)
				.configThreadPoolSize(5);

		popupView_Below = inflater.inflate(R.layout.popup_fragment_new_share,
				null);
		popupWindow_Below = new PopupWindow(popupView_Below,
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		relativeLayout_fragment = (RelativeLayout) view_new
				.findViewById(R.id.relativeLayout_fragment);
		relativeLayout_details = (RelativeLayout) view_new
				.findViewById(R.id.relativeLayout_details);
		imageView_new_back = (ImageView) view_new
				.findViewById(R.id.imageView_new_back);
		imageView_new_refresh = (ImageView) view_new
				.findViewById(R.id.imageView_new_refresh);
		textView_Tab_new = (TextView) view_new
				.findViewById(R.id.textView_Tab_new);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder mHolder;
		// 如果没有控件,就加在控件;
		// if (convertView == null) {
		mHolder = new ViewHolder();
		// 加载到布局中;
		convertView = inflater.inflate(R.layout.item_activity_new, parent,
				false);
		// 加载布局中的动态控件;
		mHolder.imageView_item_new_pic = (ImageView) convertView
				.findViewById(R.id.imageView_item_new_pic);
		mHolder.textView_item_new_title = (TextView) convertView
				.findViewById(R.id.textView_item_new_title);
		mHolder.textView_item_new_price = (TextView) convertView
				.findViewById(R.id.textView_item_new_price);
		// 加载静态点击控件;
		mHolder.imageView_item_new_share = (ImageView) convertView
				.findViewById(R.id.imageView_item_new_share);
		mHolder.checkBox_item_new_collection = (CheckBox) convertView
				.findViewById(R.id.checkBox_item_new_collection);
		mHolder.textView_item_new_share = (TextView) convertView
				.findViewById(R.id.textView_item_new_share);
		mHolder.textView_item_new_collection = (TextView) convertView
				.findViewById(R.id.textView_item_new_collection);
		// mHolder.relativeLayout_fragment = (RelativeLayout) convertView
		// .findViewById(R.id.relativeLayout_fragment);
		// mHolder.textView_new_title = (TextView) convertView
		// .findViewById(R.id.textView_new_title);

		// 加载萌萌哒的分割线;
		mHolder.textView_item_new_line = (TextView) convertView
				.findViewById(R.id.textView_item_new_line);
		// 加载图片;
		ViewUtils.inject(mHolder, convertView);
		convertView.setTag(mHolder);
		// } else {
		// mHolder = (ViewHolder) convertView.getTag();
		// }

		mHolder.textView_item_new_title.setText(list.get(position).get("title")
				+ "");
		if (list.get(position).get("price") != null) {
			mHolder.textView_item_new_price.setText("￥"
					+ list.get(position).get("price") + "");
		} else {
			mHolder.textView_item_new_price.setText("");
		}
		if (list.get(position).get("typeName") != null) {
			if (!(list.get(position).get("typeName").equals("普通商品"))) {
				mHolder.textView_item_new_price.setVisibility(View.GONE);
				mHolder.textView_item_new_share.setVisibility(View.GONE);
				mHolder.textView_item_new_collection.setVisibility(View.GONE);
				mHolder.checkBox_item_new_collection.setVisibility(View.GONE);
				mHolder.imageView_item_new_share.setVisibility(View.GONE);
			}
		}
		mHolder.textView_item_new_share.setTag(position);

		// 给listView中图片设置监听器;
		mHolder.imageView_item_new_pic
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						String itemId = list.get(position).get("itemId")
								.toString();
						String typeId = list.get(position).get("typeId")
								.toString();
						FragmentTransaction transaction = ((FragmentActivity) context)
								.getSupportFragmentManager().beginTransaction();
						Fragment fragment = new New_Detail_Fragment();
						Bundle bundle = new Bundle();
						bundle.putString("itemId", itemId);
						bundle.putString("typeId", typeId);
						fragment.setArguments(bundle);
						// mHolder.relativeLayout_fragment
						// .setVisibility(View.GONE);
						transaction.replace(R.id.relativeLayout_details,
								fragment, "100");
						// mHolder.textView_new_title.setText("宝贝详情");
						transaction.commit();
						relativeLayout_details.setVisibility(View.VISIBLE);
						relativeLayout_fragment.setVisibility(View.GONE);
						imageView_new_back.setVisibility(View.VISIBLE);
						imageView_new_refresh.setVisibility(View.GONE);
						textView_Tab_new.setText("宝贝详情");
					}
				});

		// 给分享按钮设置监听器;
		mHolder.imageView_item_new_share
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 点击时弹出popupWindow;
						popupWindow_Below.showAtLocation(popupView_Below,
								Gravity.BOTTOM, 0, 0);
					}
				});
		mHolder.textView_item_new_share
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 点击时弹出popupWindow;
						popupWindow_Below.showAtLocation(popupView_Below,
								Gravity.BOTTOM, 0, 0);
					}
				});
		mHolder.checkBox_item_new_collection
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						count++;
						if (count != 0
								&& mHolder.checkBox_item_new_collection
										.isChecked()) {
							mHolder.textView_item_new_collection
									.setText("取消收藏");
							Toast.makeText(context, "收藏成功!", Toast.LENGTH_SHORT)
									.show();
						} else if (count != 0
								&& (!mHolder.checkBox_item_new_collection
										.isChecked())) {
							mHolder.textView_item_new_collection.setText("收藏");
							Toast.makeText(context, "取消收藏成功!",
									Toast.LENGTH_SHORT).show();
						}
					}
				});
		mHolder.textView_item_new_collection
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						count++;
						if (count != 0
								&& mHolder.textView_item_new_collection
										.getText().toString().equals("收藏")) {
							mHolder.textView_item_new_collection
									.setText("取消收藏");
							mHolder.checkBox_item_new_collection
									.setChecked(true);
							Toast.makeText(context, "收藏成功!", Toast.LENGTH_SHORT)
									.show();
						} else if (count != 0
								&& mHolder.textView_item_new_collection
										.getText().toString().equals("取消收藏")) {
							mHolder.textView_item_new_collection.setText("收藏");
							mHolder.checkBox_item_new_collection
									.setChecked(false);
							Toast.makeText(context, "取消收藏成功!",
									Toast.LENGTH_SHORT).show();
						}
					}
				});
		button_popup_share_cancel = (Button) popupView_Below
				.findViewById(R.id.button_popup_share_cancel);
		button_popup_share_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popupWindow_Below.dismiss();
			}
		});

		radioButton_popup_share_pyq = (Button) popupView_Below
				.findViewById(R.id.radioButton_popup_share_pyq);
		radioButton_popup_share_wxhy = (Button) popupView_Below
				.findViewById(R.id.radioButton_popup_share_wxhy);
		radioButton_popup_share_qqhy = (Button) popupView_Below
				.findViewById(R.id.radioButton_popup_share_qqhy);
		radioButton_popup_share_wb = (Button) popupView_Below
				.findViewById(R.id.radioButton_popup_share_wb);
		radioButton_popup_share_qqkj = (Button) popupView_Below
				.findViewById(R.id.radioButton_popup_share_qqkj);

		radioButton_popup_share_pyq.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "已分享到朋友圈!", Toast.LENGTH_SHORT).show();
				popupWindow_Below.dismiss();
			}
		});
		radioButton_popup_share_wxhy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "已分享给微信好友!", Toast.LENGTH_SHORT).show();
				popupWindow_Below.dismiss();
			}
		});
		radioButton_popup_share_qqhy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "已分享给QQ好友!", Toast.LENGTH_SHORT).show();
				popupWindow_Below.dismiss();
			}
		});
		radioButton_popup_share_wb.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "已分享到新浪微博!", Toast.LENGTH_SHORT).show();
				popupWindow_Below.dismiss();
			}
		});
		radioButton_popup_share_qqkj.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "已分享到QQ空间!", Toast.LENGTH_SHORT).show();
				popupWindow_Below.dismiss();
			}
		});

		// if (list.get(position).get("typeName") != null) {
		// if (!(list.get(position).get("typeName").equals("普通商品"))) {
		// if (!list.get(position).get("picHeight").equals("0")
		// || !list.get(position).get("picWidth").equals("0")) {
		// int picHeight = Integer.parseInt(list.get(position).get(
		// "picHeight")
		// + "");
		// int picWidth = Integer.parseInt(list.get(position).get(
		// "picWidth")
		// + "");
		// bitmapUtils.configDefaultBitmapMaxSize(picWidth, picHeight);
		// }
		// }
		// }
		String imageUrl = list.get(position).get("url").toString();
		bitmapUtils.display(mHolder.imageView_item_new_pic, imageUrl);

		return convertView;
	}

	class ViewHolder {
		// 动态加载数据的控件;
		private ImageView imageView_item_new_pic;
		private TextView textView_item_new_title;
		private TextView textView_item_new_price;

		// 静态控件,需点击触发;
		private ImageView imageView_item_new_share;
		private CheckBox checkBox_item_new_collection;
		private TextView textView_item_new_share;
		private TextView textView_item_new_collection;
		// private RelativeLayout relativeLayout_fragment;
		// private TextView textView_new_title;
		// 萌萌哒的分割线;
		private TextView textView_item_new_line;
	}

	// ListView中数据是否重新加载;
	public void reloadListView(Collection<? extends Map<String, Object>> data,
			boolean isClear) {
		if (isClear) {
			list.clear();
		}
		list.addAll(data);
		notifyDataSetChanged();
	}
}
